package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.RankingInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tao on 2017/4/26.
 */

public class RankingListAdapter extends BaseAdapter{

    private List<RankingInfo> mList = new ArrayList<>();
    private Context mContext;
    public void updateData(List<RankingInfo> list, boolean isclear) {
        if (mList != null && isclear) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }
    public RankingListAdapter(Context mContext) {
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null || convertView.getTag() == null){
            viewHolder = new RankingListAdapter.ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ranking_list_item,null);
            //个人信息的标题和内容
            viewHolder.ranking_list_title = (TextView) convertView.findViewById(R.id.ranking_list_title);
            viewHolder.ranking_list_content = (TextView) convertView.findViewById(R.id.ranking_list_content);
            //奖杯image和头衔image
            viewHolder.ranking_list_Trophy = (ImageView) convertView.findViewById(R.id.ranking_list_Trophy);
            viewHolder.ranking_list_portrait = (ImageView) convertView.findViewById(R.id.ranking_list_portrait);
            //打赏数和观看数
            viewHolder.ranking_list_sparenum = (TextView) convertView.findViewById(R.id.ranking_list_sparenum);
            viewHolder.ranking_list_watchnum = (TextView) convertView.findViewById(R.id.ranking_list_watchnum);

            viewHolder.ranking_list_text = (TextView)convertView.findViewById(R.id.ranking_list_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        if (mList!=null){
            RankingInfo rankingInfo = mList.get(position);
            //个人信息的标题和内容
            if (rankingInfo.getNickName() != null){
                String name = rankingInfo.getNickName();
                try {
                    name = URLDecoder.decode(name,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                viewHolder.ranking_list_title.setText(name);
            }
            if (rankingInfo.getUsersign() != null){
                String content = rankingInfo.getUsersign();
                try {
                    content = URLDecoder.decode(content,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (content.length()>=9){
                    content.substring(0,9);
                    viewHolder.ranking_list_content.setText(content+"...");
                }else {
                    viewHolder.ranking_list_content.setText(content);
                }
            }



            //打赏数和观看数
            if (rankingInfo.getFowerCount() != null){
                viewHolder.ranking_list_sparenum.setText(rankingInfo.getFowerCount());
            }
            if (rankingInfo.getViewCount() != null){
                viewHolder.ranking_list_watchnum.setText(rankingInfo.getViewCount());
            }


            //奖杯image和头衔image
            if (rankingInfo.getUserLogo() != null){
                ImageCache.getInstance(mContext).
                        loadBitmapToView(viewHolder.ranking_list_portrait, rankingInfo.getUserLogo(),
                                new ViewUtil.Size(ViewUtil.dip2px(mContext, 85), ViewUtil.dip2px(mContext, 85)));
            }
            if (position == 0){
                viewHolder.ranking_list_Trophy.setImageResource(R.drawable.numb1);
            }else if (position == 1){
                viewHolder.ranking_list_Trophy.setImageResource(R.drawable.numb2);
            }else if (position == 2){
                viewHolder.ranking_list_Trophy.setImageResource(R.drawable.numb3);
            }else {
                viewHolder.ranking_list_Trophy.setVisibility(View.GONE);
                int numb = position+1;
                viewHolder.ranking_list_text.setText(numb+"");
                viewHolder.ranking_list_text.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }
    class ViewHolder{
        //排名
        TextView ranking_list_text;
        //个人信息的标题和内容
        TextView ranking_list_title,ranking_list_content;
        //奖杯image和头衔image
        ImageView ranking_list_Trophy,ranking_list_portrait;
        //打赏数和观看数
        TextView ranking_list_sparenum,ranking_list_watchnum;
    }
}
